package main

import (
	"net/url"
	"tool/utils"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

// URL编码
func (app *config) urlEncoding() func() {
	return func() {

		w := fyne.CurrentApp().NewWindow("URL编码")

		labelText_1 := widget.NewLabel("URL编码")
		input := widget.NewEntry()
		input.SetPlaceHolder("请输入URL编码")
		input.Wrapping = 2     // 开启自动换行
		input.MultiLine = true //多行输入

		labelText_2 := widget.NewLabel("URL解码")
		resultInput := widget.NewEntry()
		resultInput.SetPlaceHolder("请输入URL解码")
		resultInput.Wrapping = 2     // 开启自动换行
		resultInput.MultiLine = true //多行输入

		resultInput.OnChanged = func(content string) {
			//解码
			decodedURL, _ := url.QueryUnescape(content)
			input.SetText(decodedURL)

		}

		input.OnChanged = func(content string) {
			//编码
			encodedURL := url.QueryEscape(content)
			resultInput.SetText(encodedURL)
		}

		copyBtn := widget.NewButton("复制", func() {
			utils.CopyText(resultInput.Text)
		})
		emptyBtn := widget.NewButton("清空", func() {
			input.SetText("")
			resultInput.SetText("")
		})

		funcLayout := container.New(layout.NewHBoxLayout(), copyBtn, emptyBtn)
		inputLayout := container.New(layout.NewVBoxLayout(), labelText_1, input, labelText_2, resultInput, funcLayout)
		allLayout := container.NewBorder(inputLayout, nil, nil, nil, nil)
		w.SetContent(allLayout)

		w.SetFixedSize(true) // 设置固定大小，避免用户拖动改变大小

		// start 监听主窗口的关闭事件
		// 前提是必须设置主窗口
		w.SetOnClosed(func() {
			w.Close()
		})
		// end 监听主窗口的关闭事件

		// 设置窗口大小,
		w.Resize(fyne.NewSize(450, 220))
		w.CenterOnScreen()
		w.Show()
	}
}
